package md.lanjing.com.module_wallet.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_wallet.R;
import md.lanjing.com.module_wallet.R2;
import md.lanjing.com.module_wallet.bean.DrawCoinResponce;
import md.lanjing.com.module_wallet.contact.DrawCoinContact;
import retrofit2.Response;

/**
 * 提币
 */
public class DrawCoinActivity extends MvpActivity<DrawCoinContact.DrawCoinPresent> implements DrawCoinContact.IDrawCoinView {


    @BindView(R2.id.tv_purse_address)
    TextView tvPurseAddress;
    @BindView(R2.id.iv_pic)
    ImageView ivPic;
    @BindView(R2.id.tv_type_select)
    TextView tvTypeSelect;
    @BindView(R2.id.et_num)
    EditText etNum;

    String type = "USDT";
    @Override
    public void initData(Bundle savedInstanceState) {
        String purseAddress = SPUtils.getString(Constant.PURSE_ADDRESS, null, context);
        tvPurseAddress.setText(purseAddress);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_draw_coin;
    }


    @OnClick({R2.id.tv_confirm_draw_coin, R2.id.tv_type_select})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_confirm_draw_coin) {
            String numText = etNum.getText().toString();
            if (ObjectUtils.isEmpty(numText) || Double.valueOf(numText) <= 0) {
                ToastUtils.showLongToast(context, "请输入代币数量");
                return;
            }
            showDealDialog(Double.valueOf(numText));

        } else if (i == R.id.tv_type_select) {
            showTypeDialog();

        }

    }

    AlertDialog dealDialog = null;

    private void showDealDialog(double tokenNum) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = psw.getPsw();
                    mPresent.drawCoin(context, tokenNum, dealPwd, type);
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }


    AlertDialog typeDialog = null;
    private void showTypeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_type_select)
                .fromBottom(true)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(context, 80))
                .setOnClickListener(R.id.tv_one, v -> {
                    type = "USDT";
                    ivPic.setImageResource(R.drawable.icon_usdt);
                    tvTypeSelect.setText(type);
                    typeDialog.dismiss();
                }).setOnClickListener(R.id.tv_two, v -> {
                    type = "CAWD";
                    ivPic.setImageResource(R.drawable.icon_cawd);
                    tvTypeSelect.setText(type);
                    typeDialog.dismiss();
                });
        typeDialog = builder.create();
        typeDialog.show();
    }

    @Override
    public void getDrawCoinData(Response<DrawCoinResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "提币成功");
            finish();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected DrawCoinContact.DrawCoinPresent createPresent() {
        return new DrawCoinContact.DrawCoinPresent();
    }


}
